<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let maxScore = (cardPoints, k) => {
      let n = cardPoints.length, res = 0
      let s = new Array(n + 1).fill(0)
      for (let i = 0; i < n + 1; i++) {
        s[i + 1] = s[i] + cardPoints[i]
      }
      for (let i = 0; i <= k; i++) {
        res = Math.max(res, s[i] + s[n] - s[n - k + i])
      }
      return res
    };
    console.log(maxScore([1, 2, 3, 4, 5, 6, 1], 3));
  </script>
</body>

</html>